﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBS.Bll.Accounts;
using IBS.Bll.Authority;
using IBS.Bll.AutoPayments;
using IBS.Bll.Bills;
using IBS.Bll.Cards;
using IBS.Bll.KnowledgeBase;
using IBS.Bll.Payments;
using IBS.Bll.Security;
using IBS.Bll.Transfers;
using IBS.Dal;
using IBS.Model;
using IBS.ServiceContracts;

namespace IBS.Bll
{
    public class IbsContainer
    {
        #region Services
        private IIbsContext context;
        private IAuthorityService authorityService;
        public IAuthorityService AuthorityService
        {
            get
            {
                return authorityService ?? (authorityService = new AuthorityService(context, AdmissionService, KnowledgeBaseService));
            }
        }

        private IAdmissionService admissionService;
        public IAdmissionService AdmissionService { get { return admissionService ?? (admissionService = new AdmissionService()); } }
        private IKnowledgeBaseService knowledgeBaseService;
        public IKnowledgeBaseService KnowledgeBaseService { get { return knowledgeBaseService ?? (knowledgeBaseService = new KnowledgeBaseService(context, AddressService)); } }
        private IAddressService addressService;
        public IAddressService AddressService
        {
            get { return addressService ?? (addressService = new AddressService(context, StreetService)); }
        }
        private IStreetService streetService;
        public IStreetService StreetService { get { return streetService ?? (streetService = new StreetService(context)); } }

        private IAutoPaymentService autoPaymentService;
        public IAutoPaymentService AutoPaymentService
        {
            get
            {
                return autoPaymentService ?? (autoPaymentService = new AutoPaymentService(context, PaymentService, TransferService, CardService));
            }
        }

        private ITransferService transferService;
        public ITransferService TransferService { get { return transferService ?? (transferService = new TransferService(context, CurrencyService, AccountService)); } }
        private IAccountService accountService;
        public IAccountService AccountService { get { return accountService ?? (accountService = new AccountService(context, CurrencyService)); } }
        private ICurrencyService currencyService;
        public ICurrencyService CurrencyService { get { return currencyService ?? (currencyService = new CurrencyService(context)); } }
        private ICardService cardService;
        public ICardService CardService
        {
            get
            {
                return cardService ?? (cardService = new CardService(context, SecurityService, AccountService, CurrencyService, TransferService));
            }
        }

        private ISecurityService securityService;
        public ISecurityService SecurityService { get { return securityService ?? (securityService = new SecurityService(context)); } }
        private IPaymentService paymentService;
        public IPaymentService PaymentService
        {
            get
            {
                return paymentService ??
                       (paymentService = new PaymentService(context, SecurityService, AccountService, CardService, TransferService, KnowledgeBaseService));
            }
        }
        public IbsContainer()
        {
            context = new IbsContext();
        }

        private IBillService billService;
        public IBillService BillService { get { return billService ?? (billService = new BillService(context)); } }

        #endregion
        #region Processors

        public void StartAutoPaymentProcessor()
        {
            new AutoPaymentProcessor(AutoPaymentService, TransferService);
        }

        public void StartPaymentProcessor()
        {
            new PaymentProcessor(context, AccountService, CurrencyService);
        }
        #endregion
    }
}
